home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Harvest C 1.3 / Application / Examples / Harvest MiniEdit / mini.print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-17  |  3.0 KB  |  146 lines  |  [TEXT/KAHL]

  1. /*********************************************************************
  2.  
  3.     mini.print.c
  4.     
  5.     printing functions for Miniedit
  6.     
  7. *********************************************************************/
  8.  
  9. #include <Windows.h>
  10. #include <Memory.h>
  11. #include <PrintTraps.h>
  12. #include "mini.print.h"
  13. #include "pleasewait.h"
  14.  
  15. #define topMargin 20
  16. #define leftMargin 20
  17. #define bottomMargin 20
  18. #define tabChar    ((char)'\t')
  19.  
  20. static    THPrint    hPrint = NULL;
  21. static    int        tabWidth;
  22.  
  23.  
  24.  
  25.     /**
  26.      **        Prototypes for private functions.
  27.      **        (They really should be static.)
  28.      **
  29.      **/
  30.  
  31. int    CheckPrintHandle(void);
  32. int MyDrawText(char *p, int count);
  33. int PrDoc(char **hText, long count, THPrint hPrint, int font, int size);
  34. int HowMany(void);
  35.  
  36.  
  37. CheckPrintHandle()
  38. {
  39.     if (hPrint==NULL) 
  40.         PrintDefault(hPrint = (TPrint **) NewHandle(sizeof( TPrint)));
  41. }
  42.  
  43. DoPageSetUp()
  44. {
  45.     PrOpen();
  46.     CheckPrintHandle();
  47.     if (PrStlDialog(hPrint)) ;
  48.     PrClose();
  49. }
  50.  
  51.  
  52. MyDrawText(char    *p, int count)
  53. {
  54.     register char    *p1, *p2;
  55.     int                len;
  56.     Point            pt;
  57.  
  58.     p1 = p;
  59.     p2 = p+count;
  60.     while (p<p2) {
  61.         while ((p1<p2) && (*p1 !=tabChar)) *p1++;
  62.         if ((len=p1-p)>0) DrawText(p, 0, p1-p);
  63.         if (*p1==tabChar) {
  64.             GetPen(&pt);
  65.             Move((tabWidth-(pt.h-leftMargin)%tabWidth), 0);
  66.             *p1++;
  67.         }
  68.         p = p1;
  69.     }
  70. }
  71.  
  72. PrDoc (char **hText, long count, THPrint hPrint, int font, int size)
  73. {
  74.     register int     line = 0;
  75.     register int     lastLineOnPage = 0;
  76.     int                length;
  77.     Rect             printRect;
  78.     int             linesPerPage;
  79.     int             lineBase;
  80.     int             lineHeight;
  81.     register char     *ptr, *p1;
  82.     FontInfo        info;
  83.     TPPrPort        printPort;
  84.  
  85.     printPort = PrOpenDoc(hPrint, 0L, 0L);
  86.     SetPort(printPort);
  87.     TextFont(font);
  88.     TextSize(size);
  89.     printRect = (**hPrint).prInfo.rPage;
  90.     GetFontInfo(&info);
  91.     lineHeight = info.leading+info.ascent+info.descent;
  92.     linesPerPage = 
  93.         (printRect.bottom-printRect.top-topMargin-bottomMargin)/lineHeight;
  94.     HLock(hText);
  95.     ptr = p1 = (*hText);
  96.     do {
  97.         PrOpenPage(printPort, 0L);
  98.         lastLineOnPage += linesPerPage;
  99.         MoveTo( printRect.left+leftMargin, 
  100.             (lineBase = printRect.top+lineHeight) );
  101.         do {
  102.             /* PrintLine: */
  103.             while ((ptr<=(*hText)+count) && (*ptr++ != (char)'\r')) ;
  104.             if ((length=(int)(ptr-p1)-1)>0)
  105.                 MyDrawText(p1, length);
  106.             MoveTo( printRect.left+leftMargin, (lineBase += lineHeight));
  107.             p1 = ptr;
  108.         } while ((++line != lastLineOnPage) && (ptr<(*hText)+count));
  109.         PrClosePage(printPort);
  110.     } while (ptr<(*hText)+count);
  111.     HUnlock(hText);
  112.     PrCloseDoc(printPort);
  113. }
  114.  
  115. PrintText(char    **hText, long length, GrafPtr gp, int tabPixels)
  116.  
  117. {
  118.     TPPrPort    printPort;
  119.     GrafPtr        savePort;
  120.     TPrStatus    prStatus;
  121.     int            copies;
  122.     
  123.     PrOpen();
  124.     CheckPrintHandle();
  125.     tabWidth = tabPixels;
  126.     SetCursor(&qd.arrow);
  127.     if (PrJobDialog(hPrint) != 0) {
  128.         PleaseWait();
  129.         GetPort(&savePort);
  130.         for (copies=HowMany(); copies>0; copies--) {
  131.             PrDoc (hText, length, hPrint, (*gp).txFont, (*gp).txSize);
  132.             PrPicFile(hPrint, 0L, 0L, 0L, &prStatus);
  133.         }
  134.         SetPort(savePort);
  135.     }
  136.     PrClose();
  137. }
  138.  
  139. int HowMany(void)
  140. {
  141.     return( ((**hPrint).prJob.bJDocLoop==bDraftLoop) ? 
  142.                 (**hPrint).prJob.iCopies : 1 );
  143. }
  144.  
  145.  
  146.